import type { CONTAIN_SUB } from '@/enum/modules/public'
import { get, post, request } from '../ajax'
import { getAjaxBaseUrl } from '../handler'

import type {
  MeasuringToolsQueryParams,
  MeasuringToolsSaveOrUpdateParams,
} from '@/types'

const controller = getAjaxBaseUrl(
  '/measuringTools',
  '/energy',
)

/** 查询计量器具分页列表 */
export function selectMeasuringToolsPage(params: Partial<MeasuringToolsQueryParams>) {
  return post<any>(
    `${controller}/selectMeasuringToolsPage`,
    params,
  )
}

/** 新增或修改计量器具信息 */
export function saveOrUpdateMeasuringTools(data: Partial<MeasuringToolsSaveOrUpdateParams>) {
  return post(`${controller}/saveOrUpdateMeasuringTools`, data)
}

/** 查询计量器具信息 */
export function loadMeasuringTools(id: number) {
  return get(`${controller}/loadMeasuringTools`, {
    id,
  })
}

/** 删除计量器具信息 */
export function deleteMeasuringToolsById(id: number) {
  return get(`${controller}/deleteMeasuringToolsById`, {
    id,
  })
}

/** 查询计量器具树 */
export function selectMeasuringToolsTree() {
  return get(`${controller}/selectMeasuringToolsTree`)
}

/** 查询计量器具 */
export function selectMeasuringTools() {
  return get(`${controller}/selectMeasuringTools`)
}

/** 查询计量器具列表 */
export function selectMeasuringToolsList() {
  return get(`${controller}/selectMeasuringToolsList`)
}

/** 获取导入计量器具模板 */
export function getMeasuringToolsTemplate() {
  return request({
    url: `${controller}/template`,
    method: 'GET',
    responseType: 'blob',
  })
}

/** 导入计量器具信息 */
export function importMeasuringTools(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return post(`${controller}/importMeasuringTools`, formData)
}

/** 导出计量器具 */
export function exportMeasuringTools(params: {
  /** 是否包含下级 0 否，1 是  */
  containSub: CONTAIN_SUB
  energyId?: number
  pointId: number
}) {
  return post(`${controller}/exportMeasuringTools`, params)
}

/** 删除计量器具报告 */
export function deleteMeasuringToolsAnnexById(id: number) {
  return get(`${controller}/deleteReport/${id}`)
}

/** 分页查询计量器具报告 */
export function selectMeasuringToolsAnnexPage(toolId: number, pageNumber: number, pageSize: number) {
  return get(`${controller}/measuringToolsReportPage/${toolId}`, {
    pageNumber,
    pageSize,
  })
}

/** 导入计量器具报告 */
export function importMeasuringToolsAnnex(toolId: number, files: File[]) {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('files', file)
  })

  return request({
    url: `${controller}/uploadReport/${toolId}`,
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}
